package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExamServiceTyqImpl implements ExamService {
    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        String answer = "试题编号,李惠,谭雨晴,李馨,曾奕哲,车晓满,王巧家,孙晨玮,崔相元,韩睿洁\n" +
                "1,B,B,B,B,B,B,B,B,C\n" +
                "2,C,C,C,C,C,C,C,C,C\n" +
                "3,A,A,A,A,A,B,A,A,A\n" +
                "4,C,C,C,C,C,C,C,B,C\n" +
                "5,D,D,D,D,D,D,D,D,D\n" +
                "6,B,B,B,B,B,D,B,A,B\n" +
                "7,B,B,B,B,B,C,B,C,B\n" +
                "8,A,A,C,A,A,A,A,A,A\n" +
                "9,D,D,D,D,D,D,A,D,D\n" +
                "10,D,B,D,B,D,D,B,D,D\n" +
                "11,B,A,B,C,A,A,A,A,A\n" +
                "12,C,A,C,A,B,C,C,C,C\n" +
                "13,A,A,B,A,A,A,A,A,B\n" +
                "14,A,A,C,C,D,C,A,C,C\n" +
                "15,A,A,A,A,A,B,A,B,A\n" +
                "16,B,B,B,B,B,B,B,B,B\n" +
                "17,A,C,D,D,D,D,D,D,D\n" +
                "18,C,C,C,C,C,C,C,C,C\n" +
                "19,D,D,D,D,D,D,C,A,D\n" +
                "20,B,B,B,B,B,B,B,B,B\n" +
                "21,D,C,C,A,D,D,,A,\n" +
                "22,D,D,D,,D,C,,C,C\n" +
                "23,A,A,A,A,D,D,,C,C\n" +
                "24,C,C,C,C,C,D,A,C,D\n" +
                "25,B,B,B,B,B,B,,D,B";
        String[] answerRow = answer.split("\n");
        //取表头
        List<String> tableHead = new ArrayList<>(Arrays.asList(answerRow[0].split(",")));
        tableHead.remove(0);
        //取题号和答案
        List<Integer> questionNum = new ArrayList<>();

        List<List<Character>> result = new ArrayList<>();
        for (int i = 1; i < answerRow.length; i++) {
            List<Character> subResult = new ArrayList<>();
            result.add(subResult);
            String[] resultChar = answerRow[i].split(",", -1);
            questionNum.add(Integer.parseInt(resultChar[0]));
            for (int k = 1; k < resultChar.length; k++) {
                if (resultChar[k].length() == 0) {
                    result.get(i - 1).add(' ');
                } else {
                    result.get(i - 1).add(resultChar[k].charAt(0));
                }
            }
        }


        //存入answerList
        List<Answer> answers = new ArrayList<>();
        MasterDataServiceTyqImpl masterDataServiceTyq = new MasterDataServiceTyqImpl();
        QuestionBankServiceTyqImpl questionBankServiceTyq = new QuestionBankServiceTyqImpl();
//        List<Question> questionList = questionBankServiceTyq.importCorrectAnswers(" ");
        String path="D:\\索尼培训\\试题分类和考试结果.xlsx";
        List<Question> questionList=questionBankServiceTyq.importCategories(path);
        for (int i = 0; i < tableHead.size(); i++) {
            //找student
            int finalI = i;
            //Stream方法 实现匿名类查询
            Student student = masterDataServiceTyq.getStudents().stream()
                    .filter(s -> s.getName().equals(tableHead.get(finalI)))
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("找不到" + tableHead.get(finalI)));
            //Lambda方法 实现从List查询数据
//            Student student1=masterDataServiceTyq.find(masterDataServiceTyq.getStudents(), s->s.getName().equals(tableHead.get(finalI)))
//                    .orElseThrow(() -> new IllegalArgumentException("找不到"+tableHead.get(finalI)));
            //匿名类方法 实现从List查询数据
//            Student student2=masterDataServiceTyq.find(masterDataServiceTyq.getStudents(), new Predicate<Student>() {
//                @Override
//                public boolean test(Student student0) {
//                    return tableHead.get(finalI).equals(student.getName());
//                }
//            }).orElseThrow(new Supplier<IllegalArgumentException>() {
//                @Override
//                public IllegalArgumentException get() {
//                    return new IllegalArgumentException("");
//                }
//            });

            //找到学生后，找题
            for (int j = 0; j < questionNum.size(); j++) {
                int finalI1 = i;
                int finalJ = j;
                Question question = masterDataServiceTyq.find(questionList, x -> questionNum.get(finalJ) == x.getNo())
                        .orElseThrow(() -> new IllegalArgumentException("找不到" + tableHead.get(finalI1) + "的第" + questionNum.get(finalJ) + "题"));
                answers.add(new Answer(masterDataServiceTyq.getExams().get(0), question, student, result.get(j).get(i)));
            }
        }
        return answers;
    }
}
